package cn.dnui_zjq823.dnuifood_16110100823.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.adapter.FoodAdapter;
import cn.dnui_zjq823.dnuifood_16110100823.bean.CollectionBean;
import cn.dnui_zjq823.dnuifood_16110100823.bean.FoodBean;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.CollectModel;
import cn.dnui_zjq823.dnuifood_16110100823.model.SearchModel;
import cn.dnui_zjq823.dnuifood_16110100823.service.SearchService;

/**
 * Created by dell on 2018/12/6.
 */

public class SearchFragment extends BaseFragment implements onRetrofitListener{
    private RecyclerView recycler_search;
    private RecyclerView.LayoutManager layoutManager;
    private FoodAdapter foodAdapter;
    private List<FoodBean> list;
    private String search;
    private EditText edit_search;
    private Button btn_search;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecycleview(view);
        initview(view);
    }
    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==3) {
            list = (List) object;
            foodAdapter = new FoodAdapter(context);
            foodAdapter.setList(list);
            recycler_search.setAdapter(foodAdapter);
        }
    }

    @Override
    public void onFalure(String msg) {
        Toast.makeText(context, "失败："+msg, Toast.LENGTH_SHORT).show();
    }

    private void initRecycleview(View view){
        recycler_search=view.findViewById(R.id.recycler_search);
        layoutManager=new LinearLayoutManager(context);
        recycler_search.setLayoutManager(layoutManager);
    }
    private void initview(View view){
        edit_search=view.findViewById(R.id.edit_search);
        btn_search=view.findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search=edit_search.getText().toString();
                SearchModel model=new SearchModel();
                model.getFoodBySearch(search,SearchFragment.this);
            }
        });
    }
}
